<?php
/**
 * 首页相关
 *
 */
class index_model_data extends Model{
	/**
	 * 表名
	 */
	
	public function __construct(){
		parent::__construct();
	}
	
	/**
	 * 获取我最近的漫画浏览记录
	 *
	 * @param unknown_type $uid
	 */
	public function getMyReadList($uid,$num = 6){
		$sql = " SELECT a.id,a.ct_title,a.ct_cover,a.ct_flag,a.ct_nation,a.ct_lang,a.ct_chapters,a.ct_viewnum,a.ct_category,a.ct_score,b.chapter_id 
				FROM cartoon_cartoon a,cartoon_user_log b
				WHERE a.id = b.cartoon_id AND b.uid = $uid ORDER BY b.up_time DESC ".($num>0?' LIMIT '.$num:'');
		return $this->db->fetchAll($sql);
		
	}
	
	/**
	 * 添加分享
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $res_id
	 */
	public function add_share($uid,$res_id){
		if(!($uid && $res_id)) return Error::ERROR_WRONG_PARAMS;
		if($this->checkShare($uid,$res_id)){
			return Error::ERROR_RECORD_EXISTS ;
		}
		$args = array(
			'uid'	=>	$uid,
			'res_id'=>	$res_id,
			'res_type'	=> 1,
			'share_time'	=>	CURRENT_TIME
		);
		if($this->db->insert('cartoon_share',$args)){
			return true;
		}else return Error::ERROR_UNKNOWN;
	}
	
	/**
	 * 判断是否已经分享了
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $res_id
	 */
	public function checkShare($uid,$res_id){
		$sql = " SELECT id FROM cartoon_share WHERE uid = $uid AND res_id = $res_id";
		return  $this->db->fetchField($sql,'id')?true:false;
	}
	
	/**
	 * 获取用户的分享列表
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $page
	 */
	public function getShareListPages($uid,$page = NULL,$pageSize = NULL){
		if(!$uid) return false;
		$uid = is_array($uid)?implode(',',$uid):$uid;
		$sql = "SELECT * FROM cartoon_share WHERE uid IN ($uid)";
		if($page>0){
			!$pageSize && $pageSize = 10;
			return $this->db->selectPage($sql,array(),$page,$pageSize);
		}
		return $this->db->fetchAll($sql);
	}
	
	/**
	 * 根据uid获取最新分享的漫画id列表
	 *
	 * @param unknown_type $uid		//获取用户的分享
	 * @param unknown_type $num		//为最大获取数量
	 * @param unknown_type $myUid	//不为空，表示不包含我自己分享的漫画
	 */
	public function getShareList($uid = null,$num = 0,$myUid = null){
		if(!($uid || $myUid)) return false;
		$sql = "SELECT * FROM cartoon_share WHERE 1 ";
		$uid = is_array($uid)?implode(',',$uid):$uid;
		if($uid){
			$sql .= " AND uid IN ($uid)" ;
		}
		if($myUid){
			$sql.= ' AND uid != '.$myUid;
		}
		$sql.= ' ORDER BY id DESC '.($num>0)?(' LIMIT '.$num):'';
		
		return $this->db->fetchAll($sql);
	}
	
}